<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .list{
      width: 200px;
      height: 150px;
      background-color: skyblue;
      overflow: auto;
    }
    .news{
      height: 30px;
    }
  </style>
</head>
<body>
  <div id="app"></div>

  <script src="../js/17.0.1/react.development.js"></script>
  <script src="../js/17.0.1/react-dom.development.js"></script>
  <script src="../js/17.0.1/prop-types.js"></script>
  <script src="../js/17.0.1/babel.min.js"></script>
  <script type="text/babel">
    class NewsList extends React.Component{

      state = {
        newsArr:[]
      }

      componentDidMount() {
        setInterval(()=>{
          // 获取状态
          const { newsArr } = this.state
          // 模拟数据
          let news = "新闻" + (newsArr.length + 1)
          // 更新状态
          this.setState({
            newsArr:[news,...newsArr]
          })

        },1000)
      }

      getSnapshotBeforeUpdate() {
        return this.refs.list.scrollHeight
      }
      componentDidUpdate(prvProps,prvState,snapValue) {
        console.log(prvProps,prvState,snapValue);
        this.refs.list.scrollTop += this.refs.list.scrollHeight - snapValue
      }
      render() {
        return (
          <div className="list" ref="list">
            {this.state.newsArr.map((item,index)=>{
              return(
                <div className="news" key={index}>{item}</div>
              )
            })}
          </div>
        )
      }
    }

    ReactDOM.render(<NewsList />,document.getElementById("app"))
  </script>
</body>
</html>